package com.ruoyi.resume.service.impl;

import com.ruoyi.resume.enums.TaskType;
import com.ruoyi.resume.mapper.CategoryMapper;
import com.ruoyi.resume.mapper.ResumeTagMapper;
import com.ruoyi.resume.service.ResumeService;
import com.ruoyi.resume.service.TaskProcessor;
import com.ruoyi.resume.service.TaskProcessorService;
import com.ruoyi.resume.task.Task;
import com.ruoyi.resume.task.TaskQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.EnumMap;
import java.util.Map;

@Service
public class TaskProcessorServiceImpl implements TaskProcessorService {

    private final Map<TaskType, TaskProcessor> processorMap = new EnumMap<>(TaskType.class);

    @Autowired
    public TaskProcessorServiceImpl(ResumeTagMapper resumeTagMapper, ResumeService resumeService, CategoryMapper categoryMapper) {
        // 注册任务处理器
        processorMap.put(TaskType.DELETE_TAG, new DeleteTagProcessor(resumeTagMapper));
        processorMap.put(TaskType.UPDATE_TAG, new UpdateCategoriesProcessor(resumeService, categoryMapper, resumeTagMapper));
    }

    @Override
    public void processAllTasks(TaskQueue taskQueue) {
        while (!taskQueue.isEmpty()) {
            Task task = taskQueue.takeTask();
            if (task != null) {
                TaskProcessor processor = processorMap.get(task.getType());
                if (processor == null) {
                    throw new IllegalArgumentException("未知的任务类型：" + task.getType());
                }
                processor.process(task.getParam());
            }
        }
        System.out.println("任务队列处理完成");
    }
}
